<?php

namespace App\Http\Controllers\Admin\Finance;

use App\Http\Controllers\Controller;
use App\Http\Requests\finance\InvoiceRequest;
use App\Models\finance\InvoiceModel;
use App\Models\finance\PayableInvoiceModel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class PayInvoice extends Controller
{

    /**
     * Notes:列表
     * User: xiangzi
     * Date: 2024/9/5
     * Time: 14:35
     * @return \Illuminate\Http\JsonResponse
     */
    public function list(Request $request) {
        $page = $request->input('page', 1);
        $limit = $request->input('limit', 20);
        $agent_name = $request->input('agent_name', '');
        $start_date = $request->input('start_date', '');
        $end_date = $request->input('end_date', '');

        $query = PayableInvoiceModel::query()->with('create_admin');

        if($agent_name){
            $query->where('agent_name', 'like', '%'.$agent_name.'%');
        }


        if($start_date && $end_date){
            $query = $query->whereBetween('invoice_date', [$start_date, $end_date]);
        }

        $total = $query->count('id');
        $items = $query
            ->offset(($page-1) * $limit)
            ->limit($limit)
            ->orderBy('id', 'desc')
            ->get();

        $list = $items->map(function($item) {
            $res = [
                'id' => $item->id,
                'invoice_date' => $item->invoice_date,
                'agent_name' => $item->agent_name,
                'invoice_sn' => $item->invoice_sn,
                'invoice_name' => $item->invoice_name,
                'amount' => $item->amount,
                'settlement_cycle' => $item->settlement_cycle,
                'remark' => $item->remark,
                'created_at' => $item->created_at->toDateTimeString(),
                'create_user' => $item->create_admin ? $item->create_admin->staff->name: '',
            ];
            return $res;
        });

        return $this->success(['list' => $list, 'total' => $total]);
    }

    /**
     * Notes:添加
     * User: xiangzi
     * Date: 2024/9/5
     * Time: 14:35
     * @return \Illuminate\Http\JsonResponse
     */
    public function add(Request $request){
        $auth = $request->get('auth');
        $params = $request->all();

        $validator = Validator::make($params, [
            'invoice_date' => 'required',
            'agent_name' => 'required',
        ], [
            'invoice_date.required' => '请填写开票日期',
            'agent_name.required' => '请填写提供商',
        ]);

        if ($validator->fails()) {
            $err = $validator->errors()->first();
            return $this->error($err);
        }

        $model = new PayableInvoiceModel();
        $model->invoice_date = $params['invoice_date'] ?? '';
        $model->agent_name = $params['agent_name'] ?? '';
        $model->invoice_sn = $params['invoice_sn'] ?? '';
        $model->invoice_name = $params['invoice_name'] ?? '';
        $model->amount = $params['amount'] ?? 0;
        $model->settlement_cycle = $params['settlement_cycle'] ?? '';
        $model->remark = $params['remark'] ?? '';
        $model->created_admin_id = $auth->id;
        $model->save();

        return $this->success();
    }

    public function update(Request $request, $id) {
        $params = $request->all();
        $validator = Validator::make($params, [
            'invoice_date' => 'required',
            'agent_name' => 'required',
        ], [
            'invoice_date.required' => '请填写开票日期',
            'agent_name.required' => '请填写提供商',
        ]);

        if ($validator->fails()) {
            $err = $validator->errors()->first();
            return $this->error($err);
        }

        $model = PayableInvoiceModel::query()->find($id);
        if(!$model){
            return $this->error('发票不存在');
        }

        $model->invoice_date = $params['invoice_date'] ?? '';
        $model->agent_name = $params['agent_name'] ?? '';
        $model->invoice_sn = $params['invoice_sn'] ?? '';
        $model->invoice_name = $params['invoice_name'] ?? '';
        $model->amount = $params['amount'] ?? 0;
        $model->settlement_cycle = $params['settlement_cycle'] ?? '';
        $model->remark = $params['remark'] ?? '';
        $model->save();

        return $this->success();
    }

    public function delete($id) {
        $model = PayableInvoiceModel::query()->find($id);
        if(!$model){
            return $this->error('发票不存在');
        }
        $model->delete();
        return $this->success();
    }

}
